package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.PersonalAuditBean;
import com.beiing.tianshuai.tianshuai.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/1/3 0003.
 */

public class PersonalAuditAdapter extends BaseRecyclerViewAdapter<PersonalAuditBean.DataBean> {

    private static final int TYPE_THREE_COVERS_OR_NO_COVER = 0;
    private static final int TYPE_COMMON = 1;

    private Context mContext;

    private OnItemClickListener mOnItemClickListener;

    private OnContactClickListener mOnContactClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public void setOnContactClickListener(OnContactClickListener onContactClickListener) {
        mOnContactClickListener = onContactClickListener;
    }

    public PersonalAuditAdapter(Context context) {
        mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        PersonalAuditBean.DataBean bean = mList.get(position);
        String tableType = bean.getTableType();
        if (TextUtils.equals(tableType, "2")) {
            String cover = bean.getCover();
            if (TextUtils.isEmpty(cover)) {
                return TYPE_THREE_COVERS_OR_NO_COVER;
            }
            int length = cover.split(",").length;
            if (length == 0 || length > 3) {
                return TYPE_THREE_COVERS_OR_NO_COVER;
            } else if (length > 0 && length < 3) {
                return TYPE_COMMON;
            }
        } else {
            return 1;
        }
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView;
        switch (viewType) {
            case TYPE_THREE_COVERS_OR_NO_COVER:
                convertView = getConvertView(mContext, R.layout.item_personal_audit_three_covers_style, null);
                return new ThreeCoversViewHolder(convertView);
            case TYPE_COMMON:
                convertView = getConvertView(mContext, R.layout.item_personal_audit_common_style, null);
                return new CommonTypeViewHolder(convertView);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        PersonalAuditBean.DataBean bean = mList.get(position);
        if (holder instanceof CommonTypeViewHolder) {
            ((CommonTypeViewHolder) holder).setData(bean);
            ((CommonTypeViewHolder) holder).setListener(bean);
        } else if (holder instanceof ThreeCoversViewHolder) {
            ((ThreeCoversViewHolder) holder).setData(bean);
            ((ThreeCoversViewHolder) holder).setListener(bean);
        }
    }

    class CommonTypeViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.btn_contact_service)
        TextView mBtnContactService;
        @BindView(R.id.tv_state)
        TextView mTvState;
        @BindView(R.id.ic_item_type)
        TextView mIcItemType;
        @BindView(R.id.tv_create_time)
        TextView mTvCreateTime;
        @BindView(R.id.ll_header_container)
        LinearLayout mLlHeaderContainer;
        @BindView(R.id.iv_cover)
        QMUIRadiusImageView mIvCover;
        @BindView(R.id.tv_title)
        TextView mTvTitle;
        @BindView(R.id.tv_failed_reason)
        TextView mTvFailedReason;
        @BindView(R.id.rl_failed_menu_container)
        RelativeLayout mRlFailedMenuContainer;

        CommonTypeViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void setData(PersonalAuditBean.DataBean bean) {
            // item类型 0=活动 2=发现 4=兴趣
            String tableType = bean.getTableType();

            // 设置标题
            mTvTitle.setText(bean.getStrech());

            // 设置类别标签
            switch (tableType) {
                case "0":
                    mIcItemType.setText("活动");
                    break;
                case "2":
                    mIcItemType.setText("发现");
                    break;
                case "4":
                    mIcItemType.setText("兴趣");
                    break;
            }

            // 设置时间
            try {
                mTvCreateTime.setText(TimeUtils.getTime(bean.getCreateTime()));
            } catch (Exception e) {
                e.printStackTrace();
            }

            // 设置审核状态
            int controller = Integer.parseInt(bean.getController());
            controller = TextUtils.equals("0", tableType) ? controller - 1 : controller;
            switch (controller) {
                case 0:
                    mRlFailedMenuContainer.setVisibility(View.GONE);
                    mTvState.setText("审核中");
                    mTvState.setBackground(mContext.getResources().getDrawable(R.drawable.bg_audit_angle_auditing));
                    break;
                case 1:
                    mRlFailedMenuContainer.setVisibility(View.GONE);
                    mTvState.setText("已通过");
                    mTvState.setBackground(mContext.getResources().getDrawable(R.drawable.bg_audit_angle_success));
                    break;
                case 2:
                    mRlFailedMenuContainer.setVisibility(View.VISIBLE);
                    mTvFailedReason.setText(String.format("原因：%s", bean.getReason()));
                    mTvState.setText("未通过");
                    mTvState.setBackground(mContext.getResources().getDrawable(R.drawable.bg_audit_angle_failed));
                    break;
                default:
                    mRlFailedMenuContainer.setVisibility(View.GONE);
                    mTvState.setText("已推荐");
                    mTvState.setBackground(mContext.getResources().getDrawable(R.drawable.bg_audit_angle_success));
                    break;
            }

            // 设置封面
            String cover = bean.getCover();
            if (!TextUtils.isEmpty(cover)) {
                RequestOptions requestOptions = new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .centerCrop()
                        .override(690, 390);
                String[] covers = cover.split(",");
                cover = covers[0];
                switch (tableType) {
                    case "0":
                        Glide.with(mContext.getApplicationContext())
                                .load(TextUtils.isEmpty(cover) ?
                                        R.mipmap.default_avatar :
                                        HttpRequestConstant.ACTIVITY_PIC_HEAD + cover)
                                .apply(requestOptions)
                                .into(mIvCover);
                        break;
                    case "2":
                        Glide.with(mContext.getApplicationContext())
                                .load(TextUtils.isEmpty(cover) ?
                                        R.mipmap.default_avatar :
                                        HttpRequestConstant.FRESH_COVER_URL + cover)
                                .apply(requestOptions)
                                .into(mIvCover);
                        break;
                    case "4":
                        Glide.with(mContext.getApplicationContext())
                                .load(TextUtils.isEmpty(cover) ?
                                        R.mipmap.default_avatar :
                                        HttpRequestConstant.VIDEO_PIC_HEAD + cover)
                                .apply(requestOptions)
                                .into(mIvCover);
                        break;
                }
            }
        }

        public void setListener(final PersonalAuditBean.DataBean bean) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 审核状态在“已通过”状态下才可以点击查看详情
                    int controller = Integer.parseInt(bean.getController());
                    controller = TextUtils.equals("0", bean.getTableType()) ? controller - 1 : controller;
                    if (controller == 1 || controller > 2) {
                        if (mOnItemClickListener != null) {
                            mOnItemClickListener.onItemClick(bean, getAdapterPosition());
                        }
                    }
                }
            });

            mBtnContactService.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnContactClickListener != null) {
                        mOnContactClickListener.onContactClick(bean, getAdapterPosition());
                    }
                }
            });
        }
    }

    class ThreeCoversViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.btn_contact_service)
        TextView mBtnContactService;
        @BindView(R.id.ic_item_type)
        TextView mIcItemType;
        @BindView(R.id.tv_create_time)
        TextView mTvCreateTime;
        @BindView(R.id.ll_header_container)
        LinearLayout mLlHeaderContainer;
        @BindView(R.id.tv_title)
        TextView mTvTitle;
        @BindView(R.id.iv_cover_0)
        QMUIRadiusImageView mIvCover0;
        @BindView(R.id.iv_cover_1)
        QMUIRadiusImageView mIvCover1;
        @BindView(R.id.iv_cover_2)
        QMUIRadiusImageView mIvCover2;
        @BindView(R.id.ll_covers_container)
        LinearLayout mLlCoversContainer;
        @BindView(R.id.tv_state)
        TextView mTvState;
        @BindView(R.id.tv_failed_reason)
        TextView mTvFailedReason;
        @BindView(R.id.rl_failed_menu_container)
        RelativeLayout mRlFailedMenuContainer;

        ThreeCoversViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void setData(PersonalAuditBean.DataBean bean) {
            // item类型 0=活动 2=发现 4=兴趣
            String tableType = bean.getTableType();

            // 设置标题
            mTvTitle.setText(bean.getStrech());

            // 设置类别标签
            switch (tableType) {
                case "0":
                    mIcItemType.setText("活动");
                    break;
                case "2":
                    mIcItemType.setText("发现");
                    break;
                case "4":
                    mIcItemType.setText("兴趣");
                    break;
            }

            // 设置时间
            try {
                mTvCreateTime.setText(TimeUtils.getTime(bean.getCreateTime()));
            } catch (Exception e) {
                e.printStackTrace();
            }

            // 设置审核状态
            int controller = Integer.parseInt(bean.getController());
            controller = TextUtils.equals("0", tableType) ? controller - 1 : controller;
            switch (controller) {
                case 0:
                    mRlFailedMenuContainer.setVisibility(View.GONE);
                    mTvState.setText("审核中");
                    mTvState.setBackground(mContext.getResources().getDrawable(R.drawable.bg_audit_angle_auditing));
                    break;
                case 1:
                    mRlFailedMenuContainer.setVisibility(View.GONE);
                    mTvState.setText("已通过");
                    mTvState.setBackground(mContext.getResources().getDrawable(R.drawable.bg_audit_angle_success));
                    break;
                case 2:
                    mRlFailedMenuContainer.setVisibility(View.VISIBLE);
                    mTvFailedReason.setText(String.format("原因：%s", bean.getReason()));
                    mTvState.setText("未通过");
                    mTvState.setBackground(mContext.getResources().getDrawable(R.drawable.bg_audit_angle_failed));
                    break;
                default:
                    mRlFailedMenuContainer.setVisibility(View.GONE);
                    mTvState.setText("已推荐");
                    mTvState.setBackground(mContext.getResources().getDrawable(R.drawable.bg_audit_angle_success));
                    break;
            }

            // 设置封面
            String cover = bean.getCover();
            if (!TextUtils.isEmpty(cover)) {
                mLlCoversContainer.setVisibility(View.VISIBLE);
                RequestOptions requestOptions = new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .centerCrop()
                        .override(690, 390);
                String[] covers = cover.split(",");
                Glide.with(mContext.getApplicationContext())
                        .load(TextUtils.isEmpty(covers[0]) ?
                                R.mipmap.default_avatar :
                                HttpRequestConstant.FRESH_COVER_URL + covers[0])
                        .apply(requestOptions)
                        .into(mIvCover0);
                Glide.with(mContext.getApplicationContext())
                        .load(TextUtils.isEmpty(covers[1]) ?
                                R.mipmap.default_avatar :
                                HttpRequestConstant.FRESH_COVER_URL + covers[1])
                        .apply(requestOptions)
                        .into(mIvCover1);
                Glide.with(mContext.getApplicationContext())
                        .load(TextUtils.isEmpty(covers[2]) ?
                                R.mipmap.default_avatar :
                                HttpRequestConstant.FRESH_COVER_URL + covers[2])
                        .apply(requestOptions)
                        .into(mIvCover2);
            } else {
                mLlCoversContainer.setVisibility(View.GONE);
            }
        }

        public void setListener(final PersonalAuditBean.DataBean bean) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 审核状态在“已通过”状态下才可以点击查看详情
                    int controller = Integer.parseInt(bean.getController());
                    controller = TextUtils.equals("0", bean.getTableType()) ? controller - 1 : controller;
                    if (controller == 1 || controller > 2) {
                        if (mOnItemClickListener != null) {
                            mOnItemClickListener.onItemClick(bean, getAdapterPosition());
                        }
                    }
                }
            });

            mBtnContactService.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnContactClickListener != null) {
                        mOnContactClickListener.onContactClick(bean, getAdapterPosition());
                    }
                }
            });
        }
    }

    /**
     * 条目点击事件接口
     */
    public interface OnItemClickListener {
        void onItemClick(PersonalAuditBean.DataBean bean, int position);
    }

    /**
     * 联系客服点击事件接口
     */
    public interface OnContactClickListener{
        void onContactClick(PersonalAuditBean.DataBean bean, int position);
    }
}
